/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ecfdb.Acheteur;
import ecfdb.Cajot;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author QEMLAB
 */
public class CajotJpaController implements Serializable {

    public CajotJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Cajot cajot) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Acheteur acheteur = cajot.getAcheteur();
            if (acheteur != null) {
                acheteur = em.getReference(acheteur.getClass(), acheteur.getIdacheteur());
                cajot.setAcheteur(acheteur);
            }
            em.persist(cajot);
            if (acheteur != null) {
                acheteur.getCajotList().add(cajot);
                acheteur = em.merge(acheteur);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Cajot cajot) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cajot persistentCajot = em.find(Cajot.class, cajot.getIdcajot());
            Acheteur acheteurOld = persistentCajot.getAcheteur();
            Acheteur acheteurNew = cajot.getAcheteur();
            if (acheteurNew != null) {
                acheteurNew = em.getReference(acheteurNew.getClass(), acheteurNew.getIdacheteur());
                cajot.setAcheteur(acheteurNew);
            }
            cajot = em.merge(cajot);
            if (acheteurOld != null && !acheteurOld.equals(acheteurNew)) {
                acheteurOld.getCajotList().remove(cajot);
                acheteurOld = em.merge(acheteurOld);
            }
            if (acheteurNew != null && !acheteurNew.equals(acheteurOld)) {
                acheteurNew.getCajotList().add(cajot);
                acheteurNew = em.merge(acheteurNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = cajot.getIdcajot();
                if (findCajot(id) == null) {
                    throw new NonexistentEntityException("The cajot with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cajot cajot;
            try {
                cajot = em.getReference(Cajot.class, id);
                cajot.getIdcajot();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cajot with id " + id + " no longer exists.", enfe);
            }
            Acheteur acheteur = cajot.getAcheteur();
            if (acheteur != null) {
                acheteur.getCajotList().remove(cajot);
                acheteur = em.merge(acheteur);
            }
            em.remove(cajot);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Cajot> findCajotEntities() {
        return findCajotEntities(true, -1, -1);
    }

    public List<Cajot> findCajotEntities(int maxResults, int firstResult) {
        return findCajotEntities(false, maxResults, firstResult);
    }

    private List<Cajot> findCajotEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Cajot.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Cajot findCajot(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Cajot.class, id);
        } finally {
            em.close();
        }
    }

    public int getCajotCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Cajot> rt = cq.from(Cajot.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
